package cn.catsdad.util;

import cn.catsdad.constant.iConstant.ICommonWarningType;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;


@Component
@SuppressWarnings(ICommonWarningType.UNUSED)
public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    public static <T> T getBean(Class<T> beanClazz) {
        if (beanClazz == null) {
            return null;
        }
        return applicationContext.getBean(beanClazz);
    }

    public static <T> Map<String, T> getBeanOfType(Class<T> clazz) {
        return applicationContext.getBeansOfType(clazz);
    }

    public static Object getBean(String alias) {
        if (StringUtil.isEmpty(alias)) {
            return null;
        }
        return applicationContext.getBean(alias);
    }

    public static ApplicationContext getContext() {
        return applicationContext;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }
}
